package com.erp.controller;

import com.erp.annotation.Permission;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;



@RestController
@CrossOrigin
public class FileController {

    /*处理文件商品请求*/
    @Permission(role ="商品管理员")
    @PostMapping("/uploadImg")
    public String uploadImg(MultipartFile file)  {
        System.out.println("file---"+file);
        String name = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String ext= name.substring(name.lastIndexOf("."));
        String path =FileController.class.getResource("/static/imgs").getPath();
        //将图片保存到项目的imgs目录中

       // File dir=new File("/static/imgs");
      File  target=new File(path,name);

         try{
             FileUtils.copyInputStreamToFile(file.getInputStream(),target);
         }catch (Exception e){
             e.printStackTrace();
         }



        return "http://localhost:8081/imgs/"+name;
    }
}
